package org.loofer.uiframework.navigator;

import org.loofer.ext.component.logger.Logger;
import org.loofer.ext.utils.StringUtils;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;

public class NavigatorURL {
    private static final String TAG = "Navigator.URL";
    private String mHost;
    private boolean mIsValid;
    private String mPageName;
    private Map<String, String> mPageParamMap = null;
    private String mPageParams;
    private String mPageType;
    private String mPort;
    private String mSchema;

    public String getSchema() {
        return this.mSchema;
    }

    public String getHost() {
        return this.mHost;
    }

    public String getPageType() {
        return this.mPageType;
    }

    public String getPageName() {
        return this.mPageName;
    }

    public String getPageParams() {
        return this.mPageParams;
    }

    public Map<String, String> getPageParamMap() {
        return this.mPageParamMap;
    }

    public boolean isValid() {
        return this.mIsValid;
    }

    public NavigatorURL(String url) {
        String requestURL = transformToUtf8(url);
        Matcher matcher;
        if (url.startsWith(NavigatorConfig.instance().getDefaultHttpSchema())) {
            matcher = NavigatorConfig.instance().getHttpUrlPattern().matcher(requestURL);
            if (matcher.matches()) {
                this.mIsValid = true;
                this.mSchema = "http";
                int position = 1 + 1;
                this.mHost = matcher.group(1);
                int position2 = position + 1;
                this.mPort = matcher.group(position);
                position = position2 + 1;
                this.mPageType = matcher.group(position2);
                position2 = position + 1;
                this.mPageName = matcher.group(position);
                position = position2 + 1;
                this.mPageParams = matcher.group(position2);
                if (this.mPageParams != null) {
                    this.mPageParamMap = convertToParamsMap(this.mPageParams);
                    return;
                }
                return;
            } else if (NavigatorConfig.instance().getOutHttpUrlPattern().matcher(requestURL).matches()) {
                this.mIsValid = true;
                this.mSchema = "http";
                this.mPageParamMap = new HashMap();
                return;
            } else {
                this.mIsValid = false;
                return;
            }
        }
        matcher = NavigatorConfig.instance().getAppUrlPattern().matcher(requestURL);
        if (matcher.matches()) {
            this.mIsValid = true;
            this.mSchema = NavigatorConfig.instance().getAppSchema();
            position = 1 + 1;
            this.mPageType = matcher.group(1);
            position2 = position + 1;
            this.mPageName = matcher.group(position);
            position = position2 + 1;
            this.mPageParams = matcher.group(position2);
            if (this.mPageParams != null) {
                this.mPageParamMap = convertToParamsMap(this.mPageParams);
                return;
            }
            return;
        }
        this.mIsValid = false;
    }

    private Map<String, String> convertToParamsMap(String requestParams) {
        Map<String, String> formatParams = new HashMap();
        for (String param : requestParams.split("&")) {
            int pos = param.indexOf(61);
            if (pos >= 0) {
                formatParams.put(param.substring(0, pos), param.substring(pos + 1, param.length()));
            }
        }
        return formatParams;
    }

    public static String transformToUtf8(String urlString) {
        if (StringUtils.isEmpty(urlString)) {
            return "";
        }
        try {
            return URLDecoder.decode(new String(urlString.getBytes(), "UTF-8"), "UTF-8");
        } catch (UnsupportedEncodingException exception) {
            Logger.w(TAG, "decode url string error.", exception);
            return "";
        }
    }
}
